home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2809 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.6 KB  |  60 lines

  1. Newsgroups: comp.lang.c++
  2. Path: uu4news.netcom.com!amc-gw!curtis
  3. From: curtis@amc.com (Curtis Green)
  4. Subject: Re: Callbacks using member functions
  5. Message-ID: <1996Jan19.010935.12458@amc.com>
  6. Organization: Applied Microsystems Corporation
  7. X-Newsreader: TIN [version 1.1 PL6]
  8. References: <4dhge7$u9c@news.bridge.net>
  9. Date: Fri, 19 Jan 1996 01:09:35 GMT
  10.  
  11. Take a look at "Object-Oriented Programming with C++ and OSF/Motif", Douglas
  12. A. Young; Prentice-Hall; 1992; ISBN 0-13-630252-1
  13.  
  14. he goes with the premis of using a static member function as the actual
  15. callback which in turn calls a normal member function.  The this pointer
  16. is passed to the static as client_data parameter then casted.  This way the
  17. static calls the correct object version:
  18.  
  19. class foo
  20. {
  21.    static callBack( Widget, XtPointer, XtPointer);
  22.  
  23. public:
  24.    foo(Widget);
  25.  
  26.    void callme() { cout << "I was called!" << endl;
  27. }
  28.  
  29.  
  30. foo::foo(Widget w)
  31. {
  32.    XtAddCallback(w, XmNCallback, &foo::callBack, (XtPointer) this);
  33. }
  34.  
  35. foo::callBack(Widget,
  36.               XtPointer clientData,
  37.               XtPointer)
  38. {
  39.    foo *ptr = (foo *) clientData;
  40.  
  41.    ptr->callme();
  42. }
  43.  
  44.  
  45.  
  46. --
  47. ------------------------------------------------------------------------------
  48.                 |   Be seeing you...
  49.    __&__        |    
  50.   /     \       |    Curtis Green
  51.  |       |      |    Applied Microsystems Embedded Systems
  52.  |  (o)(o)      |
  53.  C   .---_)     |    (home)  gozer@halcyon.com
  54.   | |.___|      |
  55.   |  \__/       |    (work)  curtis@amc.com
  56.   /_____\       |    http://www.amc.com
  57.  /_____/ \      |
  58. /         \     |    My opinions are expressly mine on my own.
  59. ------------------------------------------------------------------------------
  60.